package ru.amse.stroganova.ui.tool.state;

import java.awt.event.MouseEvent;

import ru.amse.stroganova.presentation.EdgePresentation;
import ru.amse.stroganova.ui.GraphComponent;
import ru.amse.stroganova.ui.tool.Tool;

/**
 * Provides default select tool state.
 * 
 * @author Dasha Stroganova
 *
 */
public class DefaultState extends SelectionState {

	private boolean wasSwitchedToWeightChanging;

	/**
	 * Constructs new default state.
	 * 
	 * @param component
	 * @param parentTool
	 */
	public DefaultState(GraphComponent component, Tool parentTool) {
		super(component, parentTool);
		wasSwitchedToWeightChanging = false;
	}
	
	/*package*/ DefaultState(GraphComponent component, Tool parentTool, MouseEvent e) {
		super(component, parentTool);
		wasSwitchedToWeightChanging = false;
		mousePressed(e);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		EdgePresentation edge = component.getGraphElementSelection().getEdgeWithSelectedEnd(e.getPoint());
		if (edge != null) {
			tool.setState(new EdgeEndEditingState(component, tool, edge, component.getGraphElementSelection().isEdgeEndSelected(edge, e.getPoint())));
			return;
		}
		super.mousePressed(e);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2) {
			component.getGraphElementSelection().unselect();
			EdgePresentation selectedEdge = component.getGraphElementSelection().getEdgeWithSelectedWeight(e.getPoint());
			if (selectedEdge != null) {
				component.getGraphElementSelection().selectEdge(selectedEdge);
				wasSwitchedToWeightChanging = true;
				tool.setState(new WeightChangeState(component, tool, selectedEdge, 0));
				return;
			}
		}
		super.mouseClicked(e);
	}

	@Override
	public void finalActions() {
		if (!wasSwitchedToWeightChanging) {
			component.getGraphElementSelection().unselect();
		}
	}
}
